package bme.andraskindler.treenventory.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import bme.andraskindler.treenventory.R;
import bme.andraskindler.treenventory.activity.TreeTypeActivity;
import bme.andraskindler.treenventory.model.TypeData;
import bme.andraskindler.treenventory.provider.TreenventoryDataSource;

/**
 * @author andraskindler
 * 
 *         Új fatípus létrehozására szolgáló dialogfragment, melynek szülője a {@link TreeTypeActivity}. Megadható a latin és a magyar név, az OK gombra kattintva mentésre kerül a típus.
 * 
 */
public class NewTypeDialogFragment extends DialogFragment {

	public NewTypeDialogFragment() {
		super();
	}

	public static NewTypeDialogFragment newInstance() {
		return new NewTypeDialogFragment();
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {

		final Dialog dialog = new Dialog(getActivity());
		final View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_new_type, null);
		dialog.setContentView(view);
		dialog.setTitle("Új típus felvétele...");

		((Button) view.findViewById(R.id.treetype_add_ok)).setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				final String hunname = ((EditText) view.findViewById(R.id.treetype_add_hun)).getText().toString();
				final String latname = ((EditText) view.findViewById(R.id.treetype_add_latin)).getText().toString();
				TypeData type = new TypeData(hunname, latname);
				TreenventoryDataSource.persistType(type);
				((TreeTypeActivity) getActivity()).refreshList();
				dismiss();
			}
		});

		((Button) view.findViewById(R.id.treetype_add_cancel)).setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				dismiss();
			}
		});

		return dialog;
	}

}
